package com.baiyang.server.service.read.impl;


import com.baiyang.server.model.article.Article;
import com.baiyang.server.model.article.ArticleReadNum;
import com.baiyang.server.model.article.ArticleTypeRel;
import com.baiyang.server.model.base.ArticleType;
import com.baiyang.server.model.read.Balance;
import com.baiyang.server.model.read.Bill;
import com.baiyang.server.service.article.ArticleReadNumService;
import com.baiyang.server.service.article.ArticleService;
import com.baiyang.server.service.article.ArticleTypeRelService;
import com.baiyang.server.service.base.ArticleTypeService;
import com.baiyang.server.service.impl.BaseServiceImpl;
import com.baiyang.server.service.read.BalanceService;
import com.baiyang.server.service.read.BillService;
import com.baiyang.server.tools.DataTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baiyang.server.dao.read.ReadRecordDao;
import com.baiyang.server.model.read.ReadRecord;
import com.baiyang.server.service.read.ReadRecordService;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 
 * @author XVX
 * BY: @Primary 注解帮助生命@Autowired优先注入的实列
 */
@Service	
public class ReadRecordServiceImpl extends BaseServiceImpl<ReadRecordDao, ReadRecord> implements ReadRecordService{
    @Autowired
    private ReadRecordDao readRecordDao = null;
    @Autowired
    private BalanceService balanceService = null;
    @Autowired
    private BillService billService = null;
    @Autowired
    private ArticleService articleService = null;
    @Autowired
    private ArticleReadNumService articleReadNumService = null;
    @Autowired
    private ArticleTypeRelService articleTypeRelService;
    @Autowired
    private ArticleTypeService articleTypeService;
    /**
     * 用户阅读操作
     * 首先判断是否存在阅读记录，如果存在直接返回阅读信息，否则
     * 1.插入阅读记录
     * 2.更改余额信息
     * 3.记录账单
     * 4.增加文章阅读数量
     * 5.返回文章信息
     * @param readRecord
     * @return
     */
    @Override
    @Transactional
    public String read(ReadRecord readRecord) {
        ReadRecord record = readRecordDao.selectOne(readRecord);
        if(record == null){
            // 新增阅读记录
            readRecord.setId(DataTool.createUUID());
            readRecordDao.insert(readRecord);
            Article article = new Article();
            article.setId(readRecord.getArticleId());
            article = articleService.selectOne(article);
            int spend = (int)(article.getNum() / 100d);
            // 更改余额
            Balance balance = new Balance();
            balance.setUserId(readRecord.getUserId());
            balance = balanceService.selectOne(balance);
            balance.setBalance(balance.getBalance() - spend);
            balanceService.updateById(balance);
            // 更改账单
            Bill bill = new Bill();
            bill.setId(DataTool.createUUID());
            bill.setNum((double) spend);
            bill.setType("0");
            bill.setRelationId(readRecord.getId());
            bill.setUserId(readRecord.getUserId());
            billService.insert(bill);
            // 更改文章阅读数量
            ArticleReadNum articleReadNum = new ArticleReadNum();
            articleReadNum.setArticleId(readRecord.getArticleId());
            articleReadNum = articleReadNumService.selectOne(articleReadNum);
            articleReadNum.addNum();
            articleReadNumService.updateById(articleReadNum);
        }
        return articleService.getContent(readRecord.getArticleId());
    }

    /**
     * 判断用户余额是否可以阅读该文章
     * @param readRecord
     * @return
     */
    @Override
    public boolean canRead(ReadRecord readRecord) {
        ReadRecord record = readRecordDao.selectOne(readRecord);
        if(record!=null){
            return true;
        }
        Article article = new Article();
        article.setId(readRecord.getArticleId());
        article = articleService.selectOne(article);
        if(article==null){
            return false;
        }
        Balance balance = new Balance();
        balance.setUserId(readRecord.getUserId());
        balance = balanceService.selectOne(balance);
        if(balance == null){
            return false;
        }
        if(balance.getBalance() >= (article.getNum()/100)){
            return true;
        }else{
            return false;
        }
    }

    @Override
    public List<ReadRecord> getArticleByReadRecord(String userId, int pageStart, int pageSize) {
        List<ReadRecord> list = readRecordDao.getArticleByReadRecord(userId,pageStart,pageSize);
        list.forEach(e->{
            Article article = new Article();
            article.setId(e.getArticleId());
            article = articleService.selectOne(article);
            if(article != null){
                ArticleTypeRel rel = new ArticleTypeRel();
                rel.setArticleId(e.getArticleId());
                List<ArticleTypeRel> rels = articleTypeRelService.selectList(rel);
                List<String> ids = new ArrayList<>();
                rels.forEach(r->{
                    ids.add(r.getTypeId());
                });
                List<ArticleType> articleTypes = articleTypeService.selectByIds(ids);
                article.setArticleTypes(articleTypes);
                articleService.setDetail(article);
                e.setArticle(article);
            }
        });
        return list;
    }

    @Override
    public List<ReadRecord> selectByUserId(String userId) {
        return readRecordDao.selectByUserId(userId);
    }

    @Override
    public List<ReadRecord> selectByUserIds(List<String> ids, int i) {
        return readRecordDao.selectByUserIds(ids,i);
    }

    @Override
    public int getTodayReadNum(String id, String date) {
        return readRecordDao.getTodayReadNum(id,date);
    }
}
